home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / ts_diff.pro < prev    next >
Text File  |  1997-07-08  |  3KB  |  100 lines

  1. ;$Id: ts_diff.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       TS_DIFF
  8. ;
  9. ; PURPOSE:
  10. ;       This function recursively computes the forward differences, of an 
  11. ;       N-element time-series, K times. The result is an N-element differenced 
  12. ;       time-series with its last K elements as zeros. 
  13. ;
  14. ; CATEGORY:
  15. ;       Statistics.
  16. ;
  17. ; CALLING SEQUENCE:
  18. ;       Result = TS_Diff(X, K)
  19. ;
  20. ; INPUTS:
  21. ;       X:    An n-element vector of type integer, float or double containing
  22. ;             time-series samples.
  23. ;
  24. ;       K:    A positive scalar of type integer or long integer in the 
  25. ;             interval [1, N_ELEMENTS(X) - 1], that specifies the number of 
  26. ;             times X is differenced.
  27. ;
  28. ; KEYWORD PARAMETERS:
  29. ;     DOUBLE: If set to a non-zero value, computations are done in
  30. ;             double precision arithmetic.
  31. ;
  32. ; EXAMPLE:
  33. ;       Define an n-element vector of time-series samples.
  34. ;         x = [389, 345, 303, 362, 412, 356, 325, 375, $
  35. ;              410, 350, 310, 388, 399, 362, 325, 382, $
  36. ;              399, 382, 318, 385, 437, 357, 310, 391]
  37. ;
  38. ;       Compute the second forward differences of X.
  39. ;         result = TS_DIFF(x, 2)
  40. ;
  41. ;       The result should be:
  42. ;         [ 2, 101,   -9,  -106, 25,  81, -15, -95, $
  43. ;          20, 118,  -67,   -48,  0,  94, -40, -34, $
  44. ;         -47, 131,  -15,  -132, 33, 128,   0,   0]
  45. ;
  46. ; REFERENCE:
  47. ;       The Analysis of Time Series (Fourth Edition)
  48. ;       C. Chatfield
  49. ;       ISBN 0-412-31820-2
  50. ;
  51. ; MODIFICATION HISTORY:
  52. ;       Written by:  GGS, RSI, December 1994
  53. ;       Modified:    GGS, RSI, July 1996
  54. ;                    Added DOUBLE keywork.
  55. ;-
  56.  
  57. FUNCTION TS_Diff, x, k, Double = Double
  58.  
  59.   ON_ERROR, 2
  60.  
  61.   TypeX = SIZE(x)
  62.   nX = TypeX[TypeX[0]+2]
  63.  
  64.   if k gt nX - 1 then $
  65.     MESSAGE, "Order of differencing cannot exceed N_ELEMENTS(X) - 1."
  66.  
  67.   ;If the DOUBLE keyword is not set then the internal precision and
  68.   ;result are identical to the type of input.
  69.   if N_ELEMENTS(Double) eq 0 then Double = (TypeX[TypeX[0]+1] eq 5)
  70.  
  71.   if Double eq 0 then tsx = FLOAT([0, x]) else tsx = [0.0d, x]
  72.  
  73.   if k gt 0 then begin
  74.     for l = 1, k do begin ;Recursively compute differences.
  75.       ;j = [1, 2, 3, ..., nX-1]
  76.       ;tsx[j] = tsx[j] - tsx[j+1]
  77.       j = LINDGEN(nX-1L)+1L
  78.       tsx[j] = tsx[j] - tsx[j+1L]
  79.       tsx[nX] = 0
  80.       nX = nX - 1L
  81.     endfor
  82.     ;;endif else if k lt 0 then begin  ;Backward difference operator.
  83.     ;;  for l = 1, abs(k) do begin     ;  ts_diff(x, 1) = -ts_diff(x, -1)
  84.     ;;    a = tsx[1]                   ;  ts_diff(x, k) =  ts_diff(x, -k)
  85.     ;;    for j = 2, nX do begin       ;    for k >= 2
  86.     ;;      b = tsx[j]
  87.     ;;      tsx[j-1] = tsx[j] - a
  88.     ;;      a = b
  89.     ;;    endfor
  90.     ;;    tsx[nX] = 0.0
  91.     ;;    nX = nX - 1L
  92.     ;;  endfor
  93.     ;;endif else $
  94.   endif else $
  95.     MESSAGE, "Order of differencing must be greater than zero."
  96.  
  97.   RETURN, tsx[1:*]
  98.  
  99. END
  100.